package com.bamanker.authclient.controller;

import com.bamanker.cloudapi.utils.JsonUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author bamanker
 */


@Controller
public class IndexController {

    @GetMapping("/")
    public String root() {
        return "redirect:/index";
    }

    @GetMapping("/index")
    public String index(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

//        Map<String, Object> map = new HashMap<>();
//
//        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
//        map.put("id", auth.getName());
//
//        Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
//        List<? extends GrantedAuthority> authoritiesList = authorities.stream().collect(Collectors.toList());
//        map.put("authorities", authoritiesList);

        model.addAttribute("user", JsonUtils.objectCovertToJson(auth));
        return "index";
    }

}

